/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdStatisticalAnalysisBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.service.IsfdStatisticalAnalysisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.OutputStream;

/**
 * 统计分析
 *
 * @date: 2025/4/21 15:14
 **/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/statisticalAnalysis")
public class IsfdStatisticalAnalysisController  extends BaseController {

   private final IsfdStatisticalAnalysisService isfdStatisticalAnalysisService;
    /**
     * 数据资产统计
     */
    @PostMapping("/dataAssetStatistics")
    @Operation(summary = "数据资产统计", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:dataAssetStatistics"))
    public R<IsfdDataAssetStatisticsVo> dataAssetStatistics(@RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        return R.ok(isfdStatisticalAnalysisService.dataAssetStatistics(statisticalAnalysisBo,"page"));
    }
    /**
     * 数据增长趋势
     */
    @PostMapping("/growthTrend")
    @Operation(summary = "数据增长趋势", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:growthTrend"))
    public R<IsfdGrowthTrendVo> growthTrend(@RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        return R.ok(isfdStatisticalAnalysisService.growthTrend(statisticalAnalysisBo,"page"));
    }
    /**
     * 根据业务场景统计要素库数据量
     */
    @PostMapping("/countDataByScenario")
    @Operation(summary = "根据业务场景统计要素库数据量", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:countByScenario"))
    public R<IsfdScenarioDataVo> countDataByScenario(@RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        return R.ok(isfdStatisticalAnalysisService.countDataByScenario(statisticalAnalysisBo,"page"));
    }
    /**
     * 数据来源分布
     */
    @PostMapping("/dataSource/distribution")
    @Operation(summary = "数据来源分布", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:dataSource:distribution"))
    public R<IsfdDataSourceDistributionVo> dataSourceDistribution(@RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        return R.ok(isfdStatisticalAnalysisService.getDataSourceDistribution(statisticalAnalysisBo,"page"));
    }
    /**
     * 数据服务统计
     */
    @PostMapping("/dataServiceCount")
    @Operation(summary = "数据服务统计", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:dataServiceCount"))
    public R<IsfdDataServiceVo> dataServiceCount(@RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        return R.ok(isfdStatisticalAnalysisService.dataServiceCount(statisticalAnalysisBo,"page"));
    }
    /**
     * 存储空间统计
     */
    @PostMapping("/dataStorageCount")
    @Operation(summary = "存储空间统计", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:dataStorageCount"))
    public R<IsfdBusinessSceneMonthVo> dataStorageCount() {
//        return R.ok(isfdStatisticalAnalysisService.dataStorageCount());
        return null;
    }
    /**
     * pdf下载
     */
    @PostMapping("/pdfDownload")
    @Operation(summary = "pdf下载", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:pdfDownload"))
    public void pdfDownload(HttpServletResponse response, @RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        byte[] wordBytes = isfdStatisticalAnalysisService.pdfDownload(response,statisticalAnalysisBo);
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); // 设置响应类型为Word文档类型
        response.setHeader("Content-Disposition", "attachment; filename=\"example.docx\""); // 设置下载文件名
        try (OutputStream out = response.getOutputStream()) { // 使用try-with-resources确保资源正确关闭
            out.write(wordBytes); // 将字节流写入响应的输出流中
            out.flush(); // 刷新输出流，确保所有数据都被发送
        } catch (Exception e) {
            e.printStackTrace(); // 异常处理，实际应用中应该使用更合适的错误处理方式
        }

    }
    /**
     * 数据统计分析Redis缓存
     */
    @PostMapping("/cacheData")
    @Operation(summary = "数据统计分析Redis缓存", security = @SecurityRequirement(name = "isfd:statisticalAnalysis:dataStatistics"))
    public R<Void> cachedata(@RequestBody IsfdStatisticalAnalysisBo statisticalAnalysisBo) {
        return toAjax(isfdStatisticalAnalysisService.cachedata(statisticalAnalysisBo));
    }
}
